package com.seth.myapplication.surface;

import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seth.myapplication.R;

import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.seth.myapplication.adapter.ItemAdapter;

import com.seth.myapplication.bean.Rewords;
import com.seth.myapplication.bean.User;
import com.seth.myapplication.service.NetWorkService;
import com.seth.myapplication.utils.GlideImageLoader;
import com.seth.myapplication.utils.TasksBroadCast;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Seth on 2017/9/27.
 */
public class page1Fragment extends Fragment {

    private static final String BOARD_COST = "com.seth.myapplication.surface.page1Fragment";

    private RecyclerView mRecyclerView;
    private List<Rewords> infos = new ArrayList<>();
   // private List<String> blockInfo = new ArrayList<>();
    private ItemAdapter mMyAdapter;
   // private BlockAdapter mBlockAdapter;
    private View view;
    private static ScrollView myScrollView;
    private ItemTouchHelper helper;
    private  LinearLayoutManager linearLayoutManager;

    private LinearLayout taskType1;
    private LinearLayout taskType2;
    private LinearLayout taskType3;
    private LinearLayout taskType4;

    private Banner banner;
    private NetWorkService service;
    private TaskBroadCast msgReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        view = inflater.inflate(R.layout.first_page_fragment,container,false);
        service = new NetWorkService(getActivity(),BOARD_COST);
        initView();
        initEvent();

        return view;
    }

    @Override
    public void onAttach(Context context) {

        //动态注册广播接收器
        msgReceiver = new TaskBroadCast(getContext());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BOARD_COST);
        context.registerReceiver(msgReceiver, intentFilter);
        super.onAttach(context);
    }


    private void initEvent() {
        initData();

        setTaskTypeOnClick(taskType1,1);
        setTaskTypeOnClick(taskType2,2);
        setTaskTypeOnClick(taskType3,3);
        setTaskTypeOnClick(taskType4,4);
        //blockRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        //blockRecyclerView.setAdapter(mBlockAdapter);
       // ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(mBlockAdapter);
       // helper = new ItemTouchHelper(callback);
        //helper.attachToRecyclerView(blockRecyclerView);
        /**
         * 下面是内容展示RecyclerView，
         * 这里主要为了使ScrollView整体滑动以及item能够在ScrollView下实现match_parent
         * 因此并未绑定拖拽事件
         */
        linearLayoutManager = new LinearLayoutManager(getContext()){
            /**
             * 返回false禁止RecyclerView竖直滚动
             * @return
             */
            @Override
            public boolean canScrollVertically() {
                return false;
            }


        };

        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mMyAdapter);
        updateBanner();
        setScrollView();
    }

    private void setTaskTypeOnClick(LinearLayout taskType1, final int i) {
        taskType1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = "page";
                String url = "http://139.199.9.252/look/type";
                String data = "{\"type\":\"" +i+"\","
                        +"\"page\":\"" +1+"\","
                        +"\"count\":\"" +15+"\"}";
                service.freshTask(data,url,name);
            }
        });
    }


    private void initView() {
        myScrollView = (ScrollView) view.findViewById(R.id.scrollView);
        banner = (Banner) view.findViewById(R.id.ads_banner);
        mRecyclerView = (RecyclerView)view.findViewById(R.id.inf_show);
        taskType1 = (LinearLayout) view.findViewById(R.id.task_type1);
        taskType2 = (LinearLayout) view.findViewById(R.id.task_type2);
        taskType3 = (LinearLayout) view.findViewById(R.id.task_type3);
        taskType4 = (LinearLayout) view.findViewById(R.id.task_type4);
        //blockRecyclerView = (RecyclerView) view.findViewById(R.id.block_view);
    }

    public static void setScrollView(){

        myScrollView.smoothScrollTo(0,20);
    }

    private void initData() {

       // mList = RewordList.getRewardList();

        mMyAdapter = new ItemAdapter(getActivity());
        //mBlockAdapter = new BlockAdapter(getActivity(),blockInfo);


    }

    private void updateBanner() {
        //加载图片资源
        Integer[] images={R.mipmap.banner1,R.mipmap.banner2,R.mipmap.banner3,R.mipmap.banner4};
        List<Integer> img  = new ArrayList<>();
        for(Integer s : images){
            img.add(s);
        }
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集和
        banner.setImages(img);
        //自动轮播
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    private class TaskBroadCast extends TasksBroadCast{

        public TaskBroadCast(Context context) {
            super(context);
        }

        @Override
        protected void updateRecyclerView(int position, String update_frog) {
            mMyAdapter.notifyItemChanged(position,update_frog);
        }

        @Override
        protected void downloadPic(User usr) {
            new NetWorkService(getActivity(), BOARD_COST).downloadImage(usr.getFrog(),usr.getUsrId());
        }

        @Override
        protected void failureUpdate() {

        }

        @Override
        protected void updateRewordsUsers() {
            for(int i=0;i<rewords.size();i++){
                User user1 = users.get(rewords.get(i).getUsrId()+"");
                Log.i("UPDATE_REWORDS",user1.getUsrId()+"");
                rewords.get(i).setSeter(user1);
            }
            mMyAdapter.setList(rewords);
            mMyAdapter.notifyDataSetChanged();

        }

        @Override
        protected void updateMaps() {
            Iterator<Rewords> i = rewords.iterator();
            Log.i("ReworsdRespond","开始获取用户信息");
            //将悬赏的用户以id为key存入map
            while (i.hasNext()){
                Rewords r = i.next();
                Log.i("ReworsdRespond","用户id"+r.getUsrId());
                users.put(r.getUsrId()+"",null);
            }//获取有关key的Set
            Iterator<String> iterator = users.keySet().iterator();
            while(iterator.hasNext()){
                service.getUser(iterator.next());
                Log.i("ReworsdRespond","加载用户");
            }
        }
    }
}
